namespace IAM.Interface.Model
{
    using IAM.Interface.Common;
    using IAM.Interface.Model.ENUM;
    using IAM.Interface.Model.Interface;
    using System;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    public class Delegation : AbstractConvertObject, IConvertXml
    {
        private DateTime _begindate = DateTime.Now;
        private AccountInfo _delegator = null;
        private DateTime _enddate = DateTime.Now;
        private string _moduleid = "";
        private ModuleType _type = ModuleType.Delegate;

        public override object ConvertToObject()
        {
            this.Module = EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Module"].InnerText);
            string strType = this.CurrentXmlNode.Attributes["Type"].InnerText;
            switch (strType.ToLower())
            {
                case "delegate":
                    this.Type = ModuleType.Delegate;
                    goto Label_0097;

                case "operateas":
                    this.Type = ModuleType.OperateAs;
                    break;

                case "both":
                    this.Type = ModuleType.Both;
                    break;
            }
        Label_0097:
            this.BeginDate = Convert.ToDateTime(this.CurrentXmlNode.Attributes["BeginDate"].InnerText);
            this.EndDate = Convert.ToDateTime(this.CurrentXmlNode.Attributes["EndDate"].InnerText);
            XmlNode xn = this.CurrentXmlNode.FirstChild.FirstChild;
            this.Delegator = new AccountInfo();
            this.Delegator.CurrentXmlNode = xn;
            this.Delegator = (AccountInfo) this.Delegator.ConvertToObject();
            return this;
        }

        public override bool Equals(object obj)
        {
            Delegation delegation = null;
            try
            {
                delegation = (Delegation) obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            if (this.Module != delegation.Module)
            {
                result = false;
            }
            else if (this.Type != delegation.Type)
            {
                result = false;
            }
            else if (this.Delegator.Equals(delegation.Delegator))
            {
                result = false;
            }
            return result;
        }

        public override int GetHashCode()
        {
            int result = 1;
            foreach (PropertyInfo onofpi in base.GetType().GetProperties())
            {
                result += onofpi.GetHashCode();
            }
            return result;
        }

        public DateTime BeginDate
        {
            get
            {
                return this._begindate;
            }
            set
            {
                this._begindate = value;
            }
        }

        public AccountInfo Delegator
        {
            get
            {
                return this._delegator;
            }
            set
            {
                this._delegator = value;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return this._enddate;
            }
            set
            {
                this._enddate = value;
            }
        }

        public string Module
        {
            get
            {
                return this._moduleid;
            }
            set
            {
                this._moduleid = value;
            }
        }

        public ModuleType Type
        {
            get
            {
                return this._type;
            }
            set
            {
                this._type = value;
            }
        }

        public string XML
        {
            get
            {
                return (this.XMLBoday + this.XMLFinis);
            }
        }

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <Delegation");
                _xml.Append(" Module=\"" + EnDeCoding.Encode(this.Module) + "\"");
                _xml.Append(" Type=\"" + this.Type.ToString() + "\"");
                _xml.Append(" BeginDate=\"" + this.BeginDate.ToString() + "\"");
                _xml.Append(" EndDate=\"" + this.EndDate.ToString() + "\"");
                _xml.Append(" >");
                _xml.Append("<Delegator>");
                _xml.Append(this.Delegator.XML);
                _xml.Append("</Delegator>");
                return _xml.ToString();
            }
        }

        public string XMLFinis
        {
            get
            {
                return "</Delegation>";
            }
        }
    }
}

